<?php
/**
 * (logic)
 */
namespace App\Logic\File\Operation;

use App\Models\Operation\Business;
use App\Models\Operation\ProfitSku;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductSku;
class ParseBusiness
{
    /**
     * 解析Business
     * @param $data
     * @param $date
     * @param $countryId
     * @return int
     */
    public static function exeParse($data,$date,$countryId){
        $items = $pasArr = $pAsin = [];
        foreach($data as $k => $v){
            if($k == 0)continue;
            if(!is_array($v))continue;
            $count = count($v);
            if($count == 20){
                $parentAsin = trim($v[0]);
                if(!$parentAsin){
                    continue;
                }
                $childAsin = trim($v[1]);
                $sessionsApp = intval(str_replace(',','',trim($v[3])));
                $sessionsBrowser = intval(str_replace(',','',trim($v[4])));
                $sessionsTotal = intval(str_replace(',','',trim($v[5])));
                $pageViewsApp = intval(str_replace(',','',trim($v[9])));
                $pageViewsBrowser = intval(str_replace(',','',trim($v[10])));
                $pageViewsTotal = intval(str_replace(',','',trim($v[11])));
                $ordered = intval(str_replace(',','',trim($v[16])));
                $sales = replaceAmountChar(trim($v[18]));
                $key = $childAsin.'_'.$countryId;
                if(!array_key_exists($key,$pasArr)){
                    $pasArr[$key] = self::_getPSkuID($childAsin,$countryId);
                }
                if($pasArr[$key]){
                    if(!array_key_exists($parentAsin,$pAsin)){
                        $ps = ProductSku::getModelById($pasArr[$key]);
                        if($ps->classify_id)$pAsin[$parentAsin] = $ps->classify_id;
                    }
                }
                $items[] = ['parent_asin' => $parentAsin,
                    'child_asin' => $childAsin,
                    'sessions_app' => $sessionsApp,
                    'sessions_browser' => $sessionsBrowser,
                    'sessions_total' => $sessionsTotal,
                    'page_views_app' => $pageViewsApp,
                    'page_views_browser' => $pageViewsBrowser,
                    'page_views_total' => $pageViewsTotal,
                    'ordered' => $ordered,
                    'ordered_sales' => $sales];
            }elseif($count == 23){
                $parentAsin = trim($v[0]);
                if(!$parentAsin){
                    continue;
                }
                $childAsin = trim($v[1]);
                $sessionsApp = intval(str_replace(',','',trim($v[3])));
                $sessionsBrowser = intval(str_replace(',','',trim($v[4])));
                $sessionsTotal = intval(str_replace(',','',trim($v[5])));
                $pageViewsApp = intval(str_replace(',','',trim($v[9])));
                $pageViewsBrowser = intval(str_replace(',','',trim($v[10])));
                $pageViewsTotal = intval(str_replace(',','',trim($v[11])));
                $ordered = intval(str_replace(',','',trim($v[16])));
                $sales = replaceAmountChar(trim($v[19]));
                $key = $childAsin.'_'.$countryId;
                if(!array_key_exists($key,$pasArr)){
                    $pasArr[$key] = self::_getPSkuID($childAsin,$countryId);
                }
                if($pasArr[$key]){
                    if(!array_key_exists($parentAsin,$pAsin)){
                        $ps = ProductSku::getModelById($pasArr[$key]);
                        if($ps->classify_id)$pAsin[$parentAsin] = $ps->classify_id;
                    }
                }
                $items[] = ['parent_asin' => $parentAsin,
                    'child_asin' => $childAsin,
                    'sessions_app' => $sessionsApp,
                    'sessions_browser' => $sessionsBrowser,
                    'sessions_total' => $sessionsTotal,
                    'page_views_app' => $pageViewsApp,
                    'page_views_browser' => $pageViewsBrowser,
                    'page_views_total' => $pageViewsTotal,
                    'ordered' => $ordered,
                    'ordered_sales' => $sales];
            }elseif($count == 24){
                $parentAsin = trim($v[0]);
                if(!$parentAsin){
                    continue;
                }
                $childAsin = trim($v[1]);
                $sessionsApp = intval(str_replace(',','',trim($v[3])));
                $sessionsBrowser = intval(str_replace(',','',trim($v[4])));
                $sessionsTotal = intval(str_replace(',','',trim($v[5])));
                $pageViewsApp = intval(str_replace(',','',trim($v[9])));
                $pageViewsBrowser = intval(str_replace(',','',trim($v[10])));
                $pageViewsTotal = intval(str_replace(',','',trim($v[11])));
                $ordered = intval(str_replace(',','',trim($v[16])));
                $sales = replaceAmountChar(trim($v[20]));
                $key = $childAsin.'_'.$countryId;
                if(!array_key_exists($key,$pasArr)){
                    $pasArr[$key] = self::_getPSkuID($childAsin,$countryId);
                }
                if($pasArr[$key]){
                    if(!array_key_exists($parentAsin,$pAsin)){
                        $ps = ProductSku::getModelById($pasArr[$key]);
                        if($ps->classify_id)$pAsin[$parentAsin] = $ps->classify_id;
                    }
                }
                $items[] = ['parent_asin' => $parentAsin,
                    'child_asin' => $childAsin,
                    'sessions_app' => $sessionsApp,
                    'sessions_browser' => $sessionsBrowser,
                    'sessions_total' => $sessionsTotal,
                    'page_views_app' => $pageViewsApp,
                    'page_views_browser' => $pageViewsBrowser,
                    'page_views_total' => $pageViewsTotal,
                    'ordered' => $ordered,
                    'ordered_sales' => $sales];
            }elseif($count == 37){
                $parentAsin = trim($v[0]);
                if(!$parentAsin){
                    continue;
                }
                $childAsin = trim($v[1]);
                $sessionsApp = intval(str_replace(',','',trim($v[3])));
                $sessionsBrowser = intval(str_replace(',','',trim($v[5])));
                $sessionsTotal = intval(str_replace(',','',trim($v[7])));
                $pageViewsApp = intval(str_replace(',','',trim($v[15])));
                $pageViewsBrowser = intval(str_replace(',','',trim($v[17])));
                $pageViewsTotal = intval(str_replace(',','',trim($v[19])));
                $ordered = intval(str_replace(',','',trim($v[29])));
                $sales = replaceAmountChar(trim($v[33]));
                $key = $childAsin.'_'.$countryId;
                if(!array_key_exists($key,$pasArr)){
                    $pasArr[$key] = self::_getPSkuID($childAsin,$countryId);
                }
                if($pasArr[$key]){
                    if(!array_key_exists($parentAsin,$pAsin)){
                        $ps = ProductSku::getModelById($pasArr[$key]);
                        if($ps->classify_id)$pAsin[$parentAsin] = $ps->classify_id;
                    }
                }
                $items[] = ['parent_asin' => $parentAsin,
                    'child_asin' => $childAsin,
                    'sessions_app' => $sessionsApp,
                    'sessions_browser' => $sessionsBrowser,
                    'sessions_total' => $sessionsTotal,
                    'page_views_app' => $pageViewsApp,
                    'page_views_browser' => $pageViewsBrowser,
                    'page_views_total' => $pageViewsTotal,
                    'ordered' => $ordered,
                    'ordered_sales' => $sales];
            }
        }
        foreach ($items as $item){
            $itemKey = $item['child_asin'].'_'.$countryId;
            $item['report_date'] = $date;
            $item['country_id'] = $countryId;
            $item['psku_id'] = !array_key_exists($itemKey,$pasArr) ? 0 : $pasArr[$itemKey];
            $item['classify_id'] = !array_key_exists($item['parent_asin'],$pAsin) ? 0 : $pAsin[$item['parent_asin']];
            Business::query()->create($item);
        }
        return true;
    }

    private static function _getPSkuID($childAsin,$countryId){
        $pSkuIds = ProductAmazonSku::query()
            ->where('status',1)
            ->where('child_asin',$childAsin)
            ->where('country_id',$countryId)
            ->distinct()
            ->pluck('psku_id');
        $profit = ProfitSku::query()
            ->whereIn('psku_id',$pSkuIds)
            ->where('country_id',$countryId)
            ->where('transport',1)
            ->orderBy('profit_rate','ASC')
            ->first();
        return $profit->psku_id ?? 0;
    }
}
